<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 线下用户注册控制器
 */
class Userline extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    function reg() {

        if ($this->input->ip_address() != "120.27.142.148") {
            echo "非法访问";
            die;
        }
        $per_num = isset($_REQUEST['num']) ? $_REQUEST['num'] : 50; //指定用户注册量
        $minuter = isset($_REQUEST['minuter']) ? $_REQUEST['minuter'] : 30; //每个用户注册间隔时间（随机）
        //获取未注册用户总量
        $no_reg_users = $this->db->query("select user_id from tan_users_line where is_reg = 0 order by user_id asc")->result_array();
        if (!$no_reg_users) {
            echo "已经没有可注册的用户信息了";
            die;
        }
        $no_reg_users_ = array();
        foreach ($no_reg_users as $key => $val) {
            $no_reg_users_[$key] = $val['user_id'];
        }
        if ($per_num > count($no_reg_users_)) {
            $per_num = count($no_reg_users_);
        }

        //获取注册用户的下标
        $no_reg_users_index = array_rand($no_reg_users_, $per_num);
        if ($per_num == 1) {
            $ind = $no_reg_users_index;
            $no_reg_users_index = array();
            array_push($no_reg_users_index, $ind);
        }
        $ip = $this->input->ip_address();
        $this->time = time();
        if ($no_reg_users_index) {
            foreach ($no_reg_users_index as $index) {
                $no_user_id = $no_reg_users_[$index]; //未注册用户id
                $user = $this->db->query("select * from tan_users_line where user_id = $no_user_id and is_reg = 0 order by user_id limit 1")->row_array();
                if (!$user) {
                    continue;
                }
                $helf = ($minuter * 60) / 2;
                $second = mt_rand($helf, $minuter * 60);

                $flag = $this->users_model->get_user_flag(array("mobile" => $user['mobile']));
                //检测是否已经注册过用户
                if ($flag) {
                    $this->db->where("user_id", $user['user_id'])->update("tan_users_line", array("is_reg" => 1));
                    continue;
                }
                //生成salt
                $salt = random_string('alnum', 6);
                //生成加密密码
                $md5pass = md5(md5("tll123456") . $salt);
                $data = array();
                $data['username'] = "tll" . $user['mobile'];
                $data['password'] = $md5pass;
                $data['mobile'] = $user['mobile'];
                $data['email'] = $user['email'];
                $data['salt'] = $salt;
                $data['reg_ip'] = $ip;
                $data['reg_time'] = $this->time;
                $data['login_times'] = rand(0, 30);
                $data['is_line'] = 1;
                $data['is_setip'] = 1;

                $this->db->insert("tan_users", $data);
                $user_id = $this->db->insert_id();
                if ($user_id) {
                    //用户附加信息
                    $img_index = rand(0, 9);
                    $info_data = array();
                    $info_data['user_id'] = $user_id;
                    $info_data['truename'] = $user['true_name'];
                    $info_data['sex'] = rand(1, 2);
                    $info_data['idcard'] = $user['ID_card'];
                    $info_data['head_img'] = "http://static.tan66.com/upfiles/head/s/" . $img_index . ".jpg";
                    $info_data['head_small_img'] = "http://static.tan66.com/upfiles/head/m/" . $img_index . ".jpg";
                    $info_data['nickname'] = "tan66-" . encrypt_user_id($user_id);
                    $info_data['create_time'] = $this->time;
                    $this->users_model->insert_user_info($info_data); //插入用户附加信息

                    $this->db->where("user_id", $user['user_id'])->update("tan_users_line", array("is_reg" => 1));
                }
                $this->time = $this->time + $second;
            }
            echo "本次已全部注册完毕";
            die;
        }
    }

    //定时间导入投资数据
    function invest() {

        if ($this->input->ip_address() != "120.27.142.148") {
            echo "非法访问";
            die;
        }

        $per_num = isset($_REQUEST['num']) ? $_REQUEST['num'] : 50;
        $list = $this->db->query("select * from tan_plat_invest_list_line where is_turn_online = 0 or is_turn_online=2 order by id asc limit $per_num")->result_array();
        $ip = $this->input->ip_address();
        $this->time = time();
        
        if ($list) {
            foreach ($list as $key => $val) {
                $user = $this->db->query("select user_id,mobile from tan_users where mobile = '$val[usernamep]' order by user_id desc limit 1")->row_array();
                //123419
                if ($user) {
                    $data = array();
                    $data['user_id'] = $user['user_id'];
                    $data['plat_en'] = $val['plat_en'];
                    $data['order_nid'] = $val['order_nid'];
                    $data['borrow_nid'] = $val['borrow_nid'];
                    $data['title'] = $val['title'];
                    $data['url'] = $val['url'];
                    $data['amount'] = $val['amount'];
                    $data['invest_time'] = $val['invest_time'];
                    $data['period'] = $val['period'];
                    $data['unit'] = $val['unit'];
                    $data['rate'] = $val['rate'];
                    $data['is_online'] = 1;
                    $data['create_time'] = time();

                    $this->db->insert("tan_plat_invest_list", $data);
                    $this->db->where("id", $val['id'])->update("tan_plat_invest_list_line", array("is_turn_online" => 1));
                } else {//如果没用户生成自动注册用户
                    //生成salt
                    $salt = random_string('alnum', 6);
                    //生成加密密码
                    $md5pass = md5(md5("tll123456") . $salt);
                    $data = array();
                    $data['username'] = "tll" . $val[usernamep];
                    $data['password'] = $md5pass;
                    $data['mobile'] = $val[usernamep];
                    //$data['email'] = $user['email'];
                    $data['salt'] = $salt;
                    $data['reg_ip'] = $ip;
                    $data['reg_time'] = $this->time;
                    $data['login_times'] = rand(0, 30);
                    $data['is_line'] = 1;
                    $data['is_setip'] = 1;
                    
                    $this->db->insert("tan_users", $data);
                    $user_id = $this->db->insert_id();
                    if ($user_id) {
                        //用户附加信息
                        $img_index = rand(0, 9);
                        $info_data = array();
                        $info_data['user_id'] = $user_id;
                        $info_data['truename'] = $val[usernamep];
                        $info_data['sex'] = rand(1, 2);
                        $info_data['idcard'] = '';
                        $info_data['head_img'] = "http://static.tan66.com/upfiles/head/s/" . $img_index . ".jpg";
                        $info_data['head_small_img'] = "http://static.tan66.com/upfiles/head/m/" . $img_index . ".jpg";
                        $info_data['nickname'] = "tan66-" . encrypt_user_id($user_id);
                        $info_data['create_time'] = $this->time;
                        $this->users_model->insert_user_info($info_data); //插入用户附加信息

                        $this->db->where("user_id", $user['user_id'])->update("tan_users_line", array("is_reg" => 1));
                    }
                    $this->time = $this->time + 30;
                    
                    $data = array();
                    $data['user_id'] = $user_id;
                    $data['plat_en'] = $val['plat_en'];
                    $data['order_nid'] = $val['order_nid'];
                    $data['borrow_nid'] = $val['borrow_nid'];
                    $data['title'] = $val['title'];
                    $data['url'] = $val['url'];
                    $data['amount'] = $val['amount'];
                    $data['invest_time'] = $val['invest_time'];
                    $data['period'] = $val['period'];
                    $data['unit'] = $val['unit'];
                    $data['rate'] = $val['rate'];
                    $data['is_online'] = 1;
                    $data['create_time'] = time();

                    $this->db->insert("tan_plat_invest_list", $data);
                    $this->db->where("id", $val['id'])->update("tan_plat_invest_list_line", array("is_turn_online" => 1));
                }
            }

            echo "本次已全部转移完毕";
            die;
        } else {
            echo "没有更多数据了";
            die;
        }
    }

    //修改线下注册用户的ip
    function setip() {

        if ($_REQUEST['start'] && $_REQUEST['end']) {
            $start_date = trim($_REQUEST['start']);
            $end_date = trim($_REQUEST['end']);
        } else {
            $start_date = date("Y-m-d", strtotime("-1 day"));
            $end_date = date("Y-m-d");
        }


        $list = $this->db->query("select user_id,is_setip,mobile from tan_users where is_line = 1 and is_setip = 1 and reg_time > " . strtotime($start_date) . " and reg_time < " . strtotime($end_date) . " order by reg_time asc")->result_array();
        $count = 0;
        if ($list) {
            foreach ($list as $key => $val) {
                if (!$val['is_setip']) {
                    continue;
                }
                $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=$val[mobile]";
                $result = file_get_contents($url);
                $result = explode(",", trim($result));
                $result = explode(":", $result[1]);
                $province = trim(str_replace(array("'", " "), "", $result[1]));
                $province = iconv("gbk", 'utf-8', $province);
                $ip = $this->db->query("select ip from tan_ip where name like '%$province%' order by id desc limit 1")->row_array();
                if ($ip) {
                    $ip = explode(",", $ip['ip']);
                    $index = rand(0, count($ip) - 1);
                    $ip = $ip[$index];
                    if (!$ip) {
                        continue;
                    }
                    if ($this->db->where("user_id", $val['user_id'])->update("tan_users", array("reg_ip" => $ip, "is_setip" => 0))) {
                        $count ++;
                    }
                } else {
                    continue;
                }
            }

            echo "本次已全部处理完毕,共处理 $count 条数据";
            die;
        } else {
            echo "没有更多数据了";
            die;
        }
    }

    function rereg() {
        $pageIndex = isset($_REQUEST['page']) ? $_REQUEST['page'] : 50;
        $page = ($pageIndex - 1) * 30;
        $users = $this->db->query("select user_id,reg_time from tan_users where is_line = 1 order by user_id asc limit $page,30")->result_array();
        if ($users) {
            foreach ($users as $key => $user) {
                $second = rand(0, 30);
                $data = array();
                $data['reg_time'] = $user['reg_time'] + $second;
                $this->db->where("user_id", $user['user_id'])->update("tan_users", $data);
            }
            echo "本次已全部修改完毕";
            die;
        } else {
            echo "没有更多数据了";
            die;
        }
    }

}
